//------------------------------------------------------------------------------
// <copyright file="ComboBoxProperties.cs" company="Damian Antonowicz">
//       copyright © 2010 Damian Antonowicz
// </copyright>
// <author>Damian Antonowicz</author>
// <email>poczta@damianantonowicz.pl</email>
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Diagnostics;

namespace DotBeer.UI.WPFClient
{
    public class ComboBoxProperties
    {
        #region private members

        bool selectionChanged = false;

        #endregion

        #region properties

        public static readonly DependencyProperty ExecuteCommandAfterDropDownClosedProperty =
            DependencyProperty.RegisterAttached("ExecuteCommandAfterDropDownClosed",
                                                typeof(bool),
                                                typeof(ComboBox),
                                                new PropertyMetadata(new PropertyChangedCallback(ExecuteCommandAfterDropDownClosed_Changed)));

        #endregion

        #region constructors

        public ComboBoxProperties(ComboBox comboBox)
        {
            comboBox.DropDownOpened += new EventHandler(comboBox_DropDownOpened);
            comboBox.DropDownClosed += new EventHandler(comboBox_DropDownClosed);
            comboBox.SelectionChanged += new SelectionChangedEventHandler(comboBox_SelectionChanged);
        }

        #endregion

        #region public methods

        public static void SetExecuteCommandAfterDropDownClosed(DependencyObject d, bool execute)
        {
            d.SetValue(ExecuteCommandAfterDropDownClosedProperty, execute);
        }

        public static bool GetExecuteCommandAfterDropDownClosed(DependencyObject d)
        {
            return (bool)d.GetValue(ExecuteCommandAfterDropDownClosedProperty);
        }

        #endregion

        #region private methods

        private static void ExecuteCommandAfterDropDownClosed_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if ((bool)e.NewValue)
                new ComboBoxProperties(d as ComboBox);
        }

        private void comboBox_DropDownOpened(object sender, EventArgs e)
        {
            this.selectionChanged = false;
        }

        private void comboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.selectionChanged = true;
        }

        private void comboBox_DropDownClosed(object sender, EventArgs e)
        {
            if (this.selectionChanged)
            {
                this.selectionChanged = false;

                ComboBox comboBox = sender as ComboBox;
                ICommand command = comboBox.GetValue(CommandProperties.CommandProperty) as ICommand;

                if (command != null)
                    command.Execute(comboBox.SelectedItem);
            }
        }

        #endregion
    }
}
